<?php
/**
 * Created by PhpStorm.
 * User: hgq
 * Date: 16/6/14
 * Time: 下午1:23
 */
use yii\helpers\Url;

$web = Url::base();
$this->title = '商品分类';
?>
<script type="text/javascript" src="<?= $web ?>/js/jquery.tooltips.js"></script>
<script type="application/javascript">

    var importGoodsList = new Array();

    $(function ($) {

        //设置机构是否可选择
        var currentManager = <?=json_encode(\app\models\Manager::getCurrentManager()) ?>;
        var current_institution_code = currentManager.institution_code;


        $('#edit_image_file').change(function (event) {
            var files = event.target.files, file;
            if (files && files.length > 0) {
                file = files[0];
                console.log(file);
                if (file.size > 1024 * 1024 * 2) {
                    alert('图片大小不能超过 2MB!');
                    return false;
                }
                var URL = window.URL || window.webkitURL;
                var imgURL = URL.createObjectURL(file);
                $("#cate_image").attr("src", imgURL);
                $("#cate_image").show();
            }
        });

    });

    //同步分类(同步上级机构分类)
    function synGoodsCate() {
        var institution_code = $("#institution").val();

        if(institution_code==null || institution_code=='' || institution_code==0){
            if(!confirm("同步系统商品分类将受到刘丽敏的封杀，是否确认同步？")){
                return;
            }
        }
        var data = {};
        data.institution_code = institution_code;
        loading.show();
        requestJson("syn-goods-category-handler", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                alert('操作成功！');
                filter();
            } else {
                alert('操作失败！' + response.message);
            }
        });

    }

    //筛选
    function filter() {
        var institution_code = $("#institution").val();
        if (institution_code == null || institution_code == '') {
            institution_code = '0';
        }
        var href = window.location.href;
        href = setUrlParam(href, 'inst', institution_code);

        window.location.href = href;
    }

    function addCate(parent_code, parent_name) {
        var institution_code = $("#institution").val();
        $("#edit_code").val('');
        $("#edit_name").val('');
        if (parent_code == undefined || parent_name == undefined) {
            $("#edit_parent_code").val('');
            $("#edit_parent_name").val('');
            $("#edit_parent_name").attr("disabled", true);
        } else {
            $("#edit_parent_code").val(parent_code);
            $("#edit_parent_name").val(parent_name);
        }
        $("#edit_image_url").val('');
        $("#cate_image").attr("src", '');
        $("#edit_institution_code").val(institution_code);
        $("#continueAdd").css("display", "block")
    }


    function editCate(code, name, is_show, image_url, parent_code, institution_code) {
        $("#edit_code").val(code);
        $("#edit_name").val(name);
        $("#edit_parent_code").val(parent_code);
        $("#edit_parent_name").val('');
        var data = {};
        data.code = parent_code;
        data.institution_code = institution_code;
        requestJson("get-goods-category-handler", data, false, function (response) {
            if (response.status == 0) {
                if (response.data != null) {
                    $("#edit_parent_name").val(response.data['name']);
                }
            }
        });

        $("#edit_institution_code").val(institution_code);
        $("#edit_image_url").val(image_url);
        $("#edit_image_file").val('');
        $("#cate_image").attr("src", '');
        if (image_url != null && image_url != '') {
            $("#cate_image").attr("src", image_url);
            $("#cate_image").show();
        }
        if (is_show != null && is_show != '' && is_show==1) {
            $("#edit_is_show").bootstrapSwitch('state', true);
        }
        $("#continueAdd").css("display", "none")

    }

    function refresh() {
        var href = decodeURIComponent(window.location.href);
        window.location.href = href;
    }

    //isgoback==1保存按钮 保存数据后返回列表  //isgoback==0保存继续新增按钮 保存数据后清空当前数据
    function saveData(isgoback) {
        var name = $("#edit_name").val();
        if (name == null || name == '') {
            alert('分类名称不能为空！');
        } else {
            //FORM提交
            loading.show();
            $("#modaledit_submit_form").submit();

            $("#upload_exec_target").unbind().load(function () {
                try {
                    var result = eval('(' + ($("#upload_exec_target").contents().find("body").html()) + ')');
                    if (result.status == 0) {
                        alert("操作成功！");
                        if (isgoback == 1) {
                            refresh();
                        } else {
                            $("#edit_code").val('');
                            $("#edit_name").val('');
                            $("#edit_image_url").val('');
                            $("#edit_image_file").val('');
                            $("#cate_image").attr("src", '');
                        }
                    } else {
                        loading.hide();
                        alert(result.message);
                    }
                } catch (err) {
                    loading.hide();
                    alert("系统异常，请重试");
                }
                loading.hide();
            });

        }
    }

    //删除操作
    function delData(code, institution_code) {

        if ((code != null && code != '') && (institution_code != null && institution_code != '')) {
            var data = {};
            data.code = code;
            data.institution_code = institution_code;
            loading.show();

            requestJson("delete-goods-category-check-handler", data, true, function (response) {
                if (response.status == 0) {
                    requestJson("delete-goods-category-handler", data, true, function (response) {
                        loading.hide();
                        if (response.status == 0) {
                            loading.hide();
                            alert("操作成功！");
                            filter();
                        } else {
                            loading.hide();
                            alert("操作失败！" + response.message);
                        }
                    });
                } else {
                    loading.hide();
                    alert("操作失败！" + response.message);
                }
                loading.hide();
            });

        } else {
            alert("需删除的数据有误，请重试！");
        }
    }

    //显示下级商品种类
    function getChildGoodsCategory(obj, parentCode) {
        loading.show();
        var dataType = $(obj).attr("data-type");
        if (dataType == "open") {
            $(obj).attr("data-type", "close");
            $(obj).attr("src", "<?= $web ?>/images/mall/details_close.png");
            var data = {};
            data.institution_code = "<?=$institution_code?>";
            data.parent_code = parentCode;

            requestHtml("goods-category-child-view", data, true, function (responseHtml) {
                $(obj).parent().parent().after($(responseHtml));
                tooltipsPreview();
                loading.hide();
            })

        } else if (dataType == "close") {
            $(obj).attr("data-type", "open");
            $(obj).attr("src", "<?= $web ?>/images/mall/details_open.png");

            var childs = $("tr." + parentCode + "");
            if (childs.length > 0) {
                childs.remove();
            }
            loading.hide();
        }

    }

    //更新下级机构数据对话框
    function updateChilddata(){
        //取得选择的机构
        var institution_code = $("#institution").val();
        if(institution_code==null || institution_code==''){
            alert("请选择机构！");
            return;
        }
        updateChildInstData("<?=Url::to(['institution/institution-childlist-part-view'])?>",institution_code,2);
    }

    //确定更新下级机构数据
    function confUpdateChildData(){
        confUpdateChildInstData("<?=Url::to(['institution/sys-under-institution-data'])?>",2);
    }


    //设置模态窗 1.当用户点击模态框外部时不会关闭 2.当按下 escape 键时不会关闭 3.当初始化时不显示模态框。
    $(function () {
        $('#myModalGoodsCategoryEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });
        $('#modalChildInstList').modal({
            keyboard: false, backdrop: 'static', show: false
        });
    });

</script>
<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>
<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <button class="btn btn-primary" type="button" onclick="synGoodsCate()"><i class="fa fa-refresh"></i>
                    同步上级机构商品分类
                </button>
                <button class="btn btn-primary" type="button" onclick="addCate()"
                        data-toggle="modal"
                        data-target="#myModalGoodsCategoryEdit"><i class="fa fa-plus"></i> 新增
                </button>
                <?php if (\app\models\Permission::hasPermission("institution", "sys-under-institution-data")) {
                    ?>
                    <button class="btn btn-primary" type="button" onclick="updateChilddata()"><i class="fa fa-cog"></i>
                        更新下级机构数据
                    </button>
                    <?php
                } ?>
            </div>
            <div class="pull-right">
                <div class="form-group">
                    <label class="sr-only" for="exampleInputPassword2">机构</label>
                    <?= app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'institution', 'currentInstitutionCode' => $institution_code, 'defaultText' => '——选择机构——', 'tagSourceType'=>2]) ?>
                </div>
                <button type="button" class="btn btn-primary" onclick="filter()"><i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
</div>
<!-- body table-->
<div class="panel panel-default" id="goodsList">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th style="width:5%"></th>
                    <th style="width:5%">层级</th>
                    <th style="width:10%">分类图片</th>
                    <th style="width:60%">分类名称</th>
                    <th style="width:5%">是否显示</th>
                    <!--                    <th>父分类编码</th>-->
                    <th style="width:15%">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($list != null && $list != '') {
                    for ($i = 0; $i < count($list); $i++) {
                        ?>
                        <tr>
                            <td>
                                <img src="<?= $web ?>/images/mall/details_open.png"
                                     style="margin-top: 10px;margin-left: 5px" data-type="open"
                                     onclick="getChildGoodsCategory(this,'<?php echo $list[$i]['code'] ?>')">
                            </td>
                            <td>
                                <?php
                                echo strlen($list[$i]['code']) / 2
                                ?>
                            </td>
                            <td>
                                <a href="javascript:void(0)" class="tooltips" rel="<?= $list[$i]['image_url'] ?>"
                                   target="_blank">
                                    <img src="<?= $list[$i]['image_url'] ?>" style="width: 3rem; height: 3rem;">
                                </a>
                            </td>
                            <td><?= $list[$i]['name'] ?></td>
                            <td><?php if($list[$i]['is_show']==1) echo '是'; else echo '否'; ?></td>
                            <!--                            <td>--><?//= $list[$i]['parent_code'] ?><!--</td>-->
                            <td>
                                <div class="btn-group">
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="addCate('<?= $list[$i]['code'] ?>','<?= $list[$i]['name'] ?>')"
                                            data-toggle="modal"
                                            data-target="#myModalGoodsCategoryEdit">
                                        添加下级
                                    </button>
                                    <button class="btn btn-link btn-xs" type="button"
                                            onclick="editCate('<?= $list[$i]['code'] ?>','<?= $list[$i]['name'] ?>','<?= $list[$i]['is_show'] ?>','<?= $list[$i]['image_url'] ?>','<?= $list[$i]['parent_code'] ?>','<?= $list[$i]['institution_code'] ?>')"
                                            data-toggle="modal"
                                            data-target="#myModalGoodsCategoryEdit">
                                        修改
                                    </button>
                                    <button class="btn btn-link btn-xs" type="button"
                                            data-toggle="modal"
                                            data-target="#deleteModal"
                                            onclick="delData('<?= $list[$i]['code'] ?>','<?= $list[$i]['institution_code'] ?>')">
                                        删除
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Modal GoodsCategory Edit-->
<div class="modal fade" id="myModalGoodsCategoryEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">商品分类</h4>
            </div>
            <div id="goodscategoryedit" class="modal-body">
                <form class="form-inline" id="modaledit_submit_form" method="post" enctype="multipart/form-data"
                      action="edit-goods-category-handler" target="upload_exec_target">
                    <div class="form-group">
                        <input type="hidden" id="edit_parent_code" name="edit_parent_code" class="form-control" value=""
                               readOnly="true">
                        <label for="exampleInputPassword2">上级分类：</label>
                        <input type="text" id="edit_parent_name" name="edit_parent_name" class="form-control" value=""
                               readOnly="true" style="width: 300px">
                    </div>
                    <div class="form-group" style=" margin-top: 6px">
                        <input type="hidden" id="edit_institution_code" name="edit_institution_code"
                               class="form-control" value="" readOnly="true">


                        <label class="sr-only" for="exampleInputEmail2">分类编码：</label>
                        <input type="hidden" id="edit_code" name="edit_code" class="form-control" value=""
                               readOnly="true">


                        <label for="exampleInputPassword2">分类名称：</label>
                        <input type="text" id="edit_name" name="edit_name" class="form-control" value=""
                               style="width: 300px">
                    </div>
                    </br>
                    <div class="form-group" style=" margin-top: 6px">
                        <label for="exampleInputPassword2">是否显示：</label>
                        <div class="form-group">
                        <div class="switch">
                            <input type="checkbox" id="edit_is_show" name="edit_is_show">
                            <span
                                class="btn-clipboard popovers" data-trigger="hover"
                                data-placement="right"
                                data-content=""
                                data-original-title="提示"></span>
                        </div></div>
                    </div>
                    <div class="form-group" style=" margin-top: 6px">
<!--                        <label for="exampleInputPassword2">图片地址：</label>-->
                        <input type="hidden" id="edit_image_url" name="edit_image_url" class="form-control" value=""
                               readOnly="true">
                    </div>
                    <br/>
                    <div class="form-group" style=" margin-top: 6px">
                        <label for="exampleInputPassword2">分类图片：</label>
                        <input id="edit_image_file" name="edit_image_file" type="file" accept="image/*"
                               class="form-control" style="width: 200px;"/>
                        <img id="cate_image" name="cate_image" src="" class="img-responsive"
                             style="display: none;width:150px;margin-left: 75px;"/>
                    </div>

                </form>
                <iframe id="upload_exec_target" name="upload_exec_target" style="display:none;"></iframe>
            </div>
            <div class="modal-footer">
                <div class="btn-group">
                    <button type="button" id="continueAdd" class="btn btn-primary" onclick="saveData(0)"><i
                            class="fa fa-repeat"></i> 保存&继续新增
                    </button>
                </div>
                <button type="button" class="btn btn-primary" onclick="saveData(1)"><i class="fa fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 关闭
                </button>
            </div>
        </div>
    </div>
</div>

<!-- 下级机构选择列表-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="modalChildInstList" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <input type="hidden" id="updateChild_SuperInstCode" value="" readonly>
                <h4 class="modal-title">下级机构选择</h4>
            </div>
            <div id="childInstList" class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confUpdateChildData()"><i class="fa fa-save"></i> 确定
                </button>
                <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </button>
            </div>
        </div>
    </div>
</div>
